Conditions | 1 |
Paths | 1 |
Total Lines | 185 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* global wc_stripe_params */ |
||
4 | jQuery( function( $ ) { |
||
5 | 'use strict'; |
||
6 | |||
7 | /* Open and close for legacy class */ |
||
8 | $( 'form.checkout, form#order_review' ).on( 'change', 'input[name="wc-stripe-payment-token"]', function() { |
||
9 | if ( 'new' === $( '.stripe-legacy-payment-fields input[name="wc-stripe-payment-token"]:checked' ).val() ) { |
||
10 | $( '.stripe-legacy-payment-fields #stripe-payment-data' ).slideDown( 200 ); |
||
11 | } else { |
||
12 | $( '.stripe-legacy-payment-fields #stripe-payment-data' ).slideUp( 200 ); |
||
13 | } |
||
14 | } ); |
||
15 | |||
16 | /** |
||
17 | * Object to handle Stripe payment forms. |
||
18 | */ |
||
19 | var wc_stripe_form = { |
||
20 | |||
21 | /** |
||
22 | * Initialize event handlers and UI state. |
||
23 | */ |
||
24 | init: function() { |
||
25 | // checkout page |
||
26 | if ( $( 'form.woocommerce-checkout' ).length ) { |
||
27 | this.form = $( 'form.woocommerce-checkout' ); |
||
28 | } |
||
29 | |||
30 | $( 'form.woocommerce-checkout' ) |
||
31 | .on( |
||
32 | 'checkout_place_order_stripe', |
||
33 | this.onSubmit |
||
34 | ); |
||
35 | |||
36 | // pay order page |
||
37 | if ( $( 'form#order_review' ).length ) { |
||
38 | this.form = $( 'form#order_review' ); |
||
39 | } |
||
40 | |||
41 | $( 'form#order_review' ) |
||
42 | .on( |
||
43 | 'submit', |
||
44 | this.onSubmit |
||
45 | ); |
||
46 | |||
47 | // add payment method page |
||
48 | if ( $( 'form#add_payment_method' ).length ) { |
||
49 | this.form = $( 'form#add_payment_method' ); |
||
50 | } |
||
51 | |||
52 | $( 'form#add_payment_method' ) |
||
53 | .on( |
||
54 | 'submit', |
||
55 | this.onSubmit |
||
56 | ); |
||
57 | |||
58 | $( document ) |
||
59 | .on( |
||
60 | 'change', |
||
61 | '#wc-stripe-cc-form :input', |
||
62 | this.onCCFormChange |
||
63 | ) |
||
64 | .on( |
||
65 | 'stripeError', |
||
66 | this.onError |
||
67 | ); |
||
68 | }, |
||
69 | |||
70 | isStripeChosen: function() { |
||
71 | return $( '#payment_method_stripe' ).is( ':checked' ) && ( ! $( 'input[name="wc-stripe-payment-token"]:checked' ).length || 'new' === $( 'input[name="wc-stripe-payment-token"]:checked' ).val() ); |
||
72 | }, |
||
73 | |||
74 | hasToken: function() { |
||
75 | return 0 < $( 'input.stripe_token' ).length; |
||
76 | }, |
||
77 | |||
78 | block: function() { |
||
79 | wc_stripe_form.form.block({ |
||
80 | message: null, |
||
81 | overlayCSS: { |
||
82 | background: '#fff', |
||
83 | opacity: 0.6 |
||
84 | } |
||
85 | }); |
||
86 | }, |
||
87 | |||
88 | unblock: function() { |
||
89 | wc_stripe_form.form.unblock(); |
||
90 | }, |
||
91 | |||
92 | onError: function( e, responseObject ) { |
||
93 | var message = responseObject.response.error.message; |
||
94 | |||
95 | // Customers do not need to know the specifics of the below type of errors |
||
96 | // therefore return a generic localizable error message. |
||
97 | if ( |
||
98 | 'invalid_request_error' === responseObject.response.error.type || |
||
99 | 'api_connection_error' === responseObject.response.error.type || |
||
100 | 'api_error' === responseObject.response.error.type || |
||
101 | 'authentication_error' === responseObject.response.error.type || |
||
102 | 'rate_limit_error' === responseObject.response.error.type |
||
103 | ) { |
||
104 | message = wc_stripe_params.invalid_request_error; |
||
105 | } |
||
106 | |||
107 | if ( 'card_error' === responseObject.response.error.type && wc_stripe_params.hasOwnProperty( responseObject.response.error.code ) ) { |
||
108 | message = wc_stripe_params[ responseObject.response.error.code ]; |
||
109 | } |
||
110 | |||
111 | $( '.woocommerce-error, .stripe_token' ).remove(); |
||
112 | $( '#stripe-card-number' ).closest( 'p' ).before( '<ul class="woocommerce_error woocommerce-error"><li>' + message + '</li></ul>' ); |
||
113 | wc_stripe_form.unblock(); |
||
114 | }, |
||
115 | |||
116 | onSubmit: function( e ) { |
||
117 | if ( wc_stripe_form.isStripeChosen() && ! wc_stripe_form.hasToken() ) { |
||
118 | e.preventDefault(); |
||
119 | wc_stripe_form.block(); |
||
120 | |||
121 | var card = $( '#stripe-card-number' ).val(), |
||
122 | cvc = $( '#stripe-card-cvc' ).val(), |
||
123 | expires = $( '#stripe-card-expiry' ).payment( 'cardExpiryVal' ), |
||
124 | first_name = $( '#billing_first_name' ).length ? $( '#billing_first_name' ).val() : wc_stripe_params.billing_first_name, |
||
125 | last_name = $( '#billing_last_name' ).length ? $( '#billing_last_name' ).val() : wc_stripe_params.billing_last_name, |
||
126 | data = { |
||
127 | number : card, |
||
128 | cvc : cvc, |
||
129 | exp_month: parseInt( expires.month, 10 ) || 0, |
||
130 | exp_year : parseInt( expires.year, 10 ) || 0 |
||
131 | }; |
||
132 | |||
133 | if ( first_name && last_name ) { |
||
134 | data.name = first_name + ' ' + last_name; |
||
135 | } |
||
136 | |||
137 | if ( $( '#billing_address_1' ).length > 0 ) { |
||
138 | data.address_line1 = $( '#billing_address_1' ).val(); |
||
139 | data.address_line2 = $( '#billing_address_2' ).val(); |
||
140 | data.address_state = $( '#billing_state' ).val(); |
||
141 | data.address_city = $( '#billing_city' ).val(); |
||
142 | data.address_zip = $( '#billing_postcode' ).val(); |
||
143 | data.address_country = $( '#billing_country' ).val(); |
||
144 | } else if ( wc_stripe_params.billing_address_1 ) { |
||
145 | data.address_line1 = wc_stripe_params.billing_address_1; |
||
146 | data.address_line2 = wc_stripe_params.billing_address_2; |
||
147 | data.address_state = wc_stripe_params.billing_state; |
||
148 | data.address_city = wc_stripe_params.billing_city; |
||
149 | data.address_zip = wc_stripe_params.billing_postcode; |
||
150 | data.address_country = wc_stripe_params.billing_country; |
||
151 | } |
||
152 | |||
153 | Stripe.createToken( data, wc_stripe_form.onStripeResponse ); |
||
154 | |||
155 | // Prevent form submitting |
||
156 | return false; |
||
157 | } |
||
158 | }, |
||
159 | |||
160 | onCCFormChange: function() { |
||
161 | $( '.woocommerce-error, .stripe_token' ).remove(); |
||
162 | }, |
||
163 | |||
164 | onStripeResponse: function( status, response ) { |
||
165 | if ( response.error ) { |
||
166 | $( document ).trigger( 'stripeError', { response: response } ); |
||
167 | } else { |
||
168 | // check if we allow prepaid cards |
||
169 | if ( 'no' === wc_stripe_params.allow_prepaid_card && 'prepaid' === response.card.funding ) { |
||
170 | response.error = { message: wc_stripe_params.no_prepaid_card_msg }; |
||
171 | |||
172 | $( document ).trigger( 'stripeError', { response: response } ); |
||
173 | |||
174 | return false; |
||
175 | } |
||
176 | |||
177 | // token contains id, last4, and card type |
||
178 | var token = response.id; |
||
179 | |||
180 | // insert the token into the form so it gets submitted to the server |
||
181 | wc_stripe_form.form.append( "<input type='hidden' class='stripe_token' name='stripe_token' value='" + token + "'/>" ); |
||
182 | wc_stripe_form.form.submit(); |
||
183 | } |
||
184 | } |
||
185 | }; |
||
186 | |||
187 | wc_stripe_form.init(); |
||
188 | } ); |
||
189 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.